package com.bestcem.xm.ticket.dao.converter;

import com.bestcem.xm.ticket.entity.mongo.TicketRecord;
import com.bestcem.xm.ticket.entity.mysql.TicketRecordDO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/22 15:01
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TicketRecordConverter {

    @Nullable
    public static TicketRecord doToDto(TicketRecordDO ticketRecordDO) {
        if (Objects.isNull(ticketRecordDO)) {
            return null;
        }

        TicketRecord ticketRecord = new TicketRecord();
        ticketRecord.setId(ticketRecordDO.getTicketRecordId());
        ticketRecord.setOrgId(ticketRecordDO.getOrgId());
        ticketRecord.setUserId(ticketRecordDO.getUserId());
        ticketRecord.setTitle(ticketRecordDO.getTitle());
        ticketRecord.setTicketId(ticketRecordDO.getTicketId());
        ticketRecord.setRoleId(ticketRecordDO.getRoleId());
        ticketRecord.setRegion(ticketRecordDO.getRegion());
        ticketRecord.setRegionName(ticketRecordDO.getRegionName());
        ticketRecord.setUserName(ticketRecordDO.getUserName());
        ticketRecord.setSource(ticketRecordDO.getSource());
        ticketRecord.setEvent(ticketRecordDO.getEvent());
        ticketRecord.setCustomAttr(CommonConverter
                .stringToObject(ticketRecordDO.getCustomAttr(), TicketRecord.CustomAttr.class));
        ticketRecord.setSurveyId(ticketRecordDO.getSurveyId());
        ticketRecord.setTicketStatus(ticketRecordDO.getTicketStatus());
        ticketRecord.setReason(ticketRecordDO.getReason());
        ticketRecord.setReceiveUserId(ticketRecordDO.getReceiveUserId());
        ticketRecord.setReceiveUserName(ticketRecordDO.getReceiveUserName());
        ticketRecord.setReceiveRoleId(ticketRecordDO.getReceiveRoleId());
        ticketRecord.setReceiveRoleName(ticketRecordDO.getReceiveRoleName());
        ticketRecord.setReceiveRegionName(ticketRecordDO.getReceiveRegionName());
        ticketRecord.setReceiveRegionId(ticketRecordDO.getReceiveRegionId());
        ticketRecord.setSample(ticketRecordDO.getSample());

        CommonConverter.setEntity(ticketRecord, ticketRecordDO);
        return ticketRecord;
    }

    @Nonnull
    public static List<TicketRecord> doListToDtoList(List<TicketRecordDO> ticketRecordDOList) {
        List<TicketRecord> list = new ArrayList<>();
        if (Objects.isNull(ticketRecordDOList)) {
            return list;
        }

        for (TicketRecordDO ticketRecordDO : ticketRecordDOList) {
            TicketRecord ticketRecord = doToDto(ticketRecordDO);
            list.add(ticketRecord);
        }
        return list;
    }

    @Nullable
    public static TicketRecordDO dtoToDo(TicketRecord ticketRecord) {
        if (Objects.isNull(ticketRecord)) {
            return null;
        }

        TicketRecordDO ticketRecordDO = new TicketRecordDO();
        ticketRecordDO.setTicketRecordId(ticketRecord.getId());
        ticketRecordDO.setOrgId(ticketRecord.getOrgId());
        ticketRecordDO.setUserId(ticketRecord.getUserId());
        ticketRecordDO.setTitle(ticketRecord.getTitle());
        ticketRecordDO.setTicketId(ticketRecord.getTicketId());
        ticketRecordDO.setRoleId(ticketRecord.getRoleId());
        ticketRecordDO.setRegion(ticketRecord.getRegion());
        ticketRecordDO.setRegionName(ticketRecord.getRegionName());
        ticketRecordDO.setUserName(ticketRecord.getUserName());
        ticketRecordDO.setSource(ticketRecord.getSource());
        ticketRecordDO.setEvent(ticketRecord.getEvent());
        ticketRecordDO.setCustomAttr(CommonConverter.objectToString(ticketRecord.getCustomAttr()));
        ticketRecordDO.setSurveyId(ticketRecord.getSurveyId());
        ticketRecordDO.setTicketStatus(ticketRecord.getTicketStatus());
        ticketRecordDO.setReason(ticketRecord.getReason());
        ticketRecordDO.setReceiveUserId(ticketRecord.getReceiveUserId());
        ticketRecordDO.setReceiveUserName(ticketRecord.getReceiveUserName());
        ticketRecordDO.setReceiveRoleId(ticketRecord.getReceiveRoleId());
        ticketRecordDO.setReceiveRoleName(ticketRecord.getReceiveRoleName());
        ticketRecordDO.setReceiveRegionName(ticketRecord.getReceiveRegionName());
        ticketRecordDO.setReceiveRegionId(ticketRecord.getReceiveRegionId());
        ticketRecordDO.setSample(ticketRecord.getSample());

        CommonConverter.setDo(ticketRecord, ticketRecordDO);
        return ticketRecordDO;
    }
}
